package functions.ems_accountant_job_pool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.crc.mam.BaseCase;
import coms.AutoException;
import coms.Constants;
import coms.TestDateString;
import frame.HttpHelper;
import functions.mams_rest.Sys;

/**
 * 会计任务池--设置工作日历，判断执行的那天是否是工作日，如果不是设置为工作日
 * @author CHENHAILIAN6
 *
 */
public class AccountantCalendar extends BaseCase{
  private org.slf4j.Logger logger = LoggerFactory.getLogger(AccountantCalendar.class);
  
  public JSONArray findAllAccountantCalendar() {
    
    String startDate = TestDateString.afterDay(-1);
    String endDate = TestDateString.afterDay(1);
    /**
     * ACCOUNT_CALENDAR_DEFAULT_WORKTIME 查询工作时间
     * 需要调用Sys里面的方法
     */
    Sys sys = new Sys();
    JSONArray timesArray = sys.getValue("ACCOUNT_CALENDAR_DEFAULT_WORKTIME");
    String workStime = "";
    String workStime2 = "";
    String workEtime = "";
    String workEtime2 = "";
    for(int i =0;i<timesArray.length();i++) {
      JSONObject object = (JSONObject) timesArray.get(i);
      if(object.getString("vsiKey").equals("workStime")) {
        workStime = object.getString("vsiValue");
      }else if(object.getString("vsiKey").equals("workStime2")) {
        workStime2 = object.getString("vsiValue");
      }else if(object.getString("vsiKey").equals("workEtime")) {
        workEtime = object.getString("vsiValue");
      }else if(object.getString("vsiKey").equals("workEtime2")) {
        workEtime2 = object.getString("vsiValue");
      }
    }
      
    String params = "{\"startDate\":\""+startDate+"\",\"endDate\":\""+endDate+"\","
        + "\"workStime\":\""+workStime+"\",\"workStime2\":\""+workStime2+"\","
            + "\"workEtime\":\""+workEtime+"\",\"workEtime2\":\""+workEtime2+"\"}";
    String url = Constants.FIND_ALL_ACCOUNTANT_CALENDAR_URL;
    try {
      result = HttpHelper.post(params, url);
      logger.info("findAllAccountantCalendar --- " + result);
      status = HttpHelper.isOk;
      if (status) {
         try {
           resultJSONArray = new JSONArray(result);
         }catch(JSONException e) {
           result = "result 返回格式错误，无法解析成数组";
         }
      }else {
        remark = "查询设置的工作日历失败";
        throw new AutoException("查询设置的工作日历失败");
      }
    }catch(Exception e) {
      status = false;
      remark = "查询设置的工作日历失败";
      throw new AutoException("查询设置的工作日历失败");
    }
    return resultJSONArray;
  }
  
  /**
   * 设置工作日历为工作日非工作日无效  0 为非工作日，1为工作日
   * @param date
   * @param workFlag
   */
  public void setAccountantCalendarWorkFlag(String date,String workFlag) {
   
    String params = "{\"workDate\":\""+date+"\",\"workFlag\":\""+workFlag+"\"}";
    String url = Constants.SET_ACCOUNTANT_CALENDAR_WORK_FLAG_URL;
    try {
      result = HttpHelper.post(params, url);
      logger.info("setAccountantCalendarWorkFlag --- " + result);
      status = HttpHelper.isOk;
      if (status) {
        try {
          JSONObject object = new JSONObject(result);
          String workDate = object.getString("workDate");
          String recordWorkFlag = object.getString("workFlag");         
          if(workDate.equals(date) && recordWorkFlag.equals(workFlag)) {
            logger.info("添加上班时间成功");
          }else {
            status = false;
            remark = "添加上班时间失败";
            logger.error("添加上班时间失败");
          }
        }catch(JSONException e) {
          throw new AutoException("解析成json对象时出错");
        }
      }else {
        
        throw new AutoException("设置工作日历为非工作日/工作日失败");
      }
    }catch(Exception e) {
      throw new AutoException("设置工作日历为非工作日/工作日失败");
    }
  }
  
  /**
   * 设置工作时间
   * @param date
   * @param workSTime
   * @param workSTime2
   * @param workETime
   * @param workETime2
   */
  public void setAccountantCalendarAllWorkTime(String date,String workStime,
      String workStime2,String workEtime,String workEtime2) {
    
     String params = "{\"workDate\":\""+date+"\",\"workStime\":\""+workStime+"\","
        + "\"workEtime\":\""+workEtime+"\",\"workStime2\":\""+workStime2+ "\",\"workEtime2\":\""+workEtime2+"\"}";
     String url = Constants.SET_ACCOUNTANT_CALENDAR_ALL_WORK_TIME_URL;
     logger.info("setAccountantCalendarAllWorkTime params ---- " + params);
     try {
       result = HttpHelper.post(params, url);
       logger.info("setAccountantCalendarWorkFlag --- " + result);
       status = HttpHelper.isOk;
       if (status) {
         try {
           JSONObject object = new JSONObject(result);
           String workDate = object.getString("workDate");
           String recordWorkSTime = object.getString("workStime");
           String recordWorkSTime2 = object.getString("workStime2");
           String recordWorkETime = object.getString("workEtime");
           String recordWorkETime2 = object.getString("workEtime2");  
         /*  logger.info(workDate + ", " + workDate.equals(date));
           logger.info(recordWorkSTime + ", " + recordWorkSTime.equals(workStime));
           logger.info(recordWorkSTime2 + ", " + recordWorkSTime2.equals(workStime2) );
           logger.info(recordWorkETime + ", " + recordWorkETime.equals(workEtime)  );
           logger.info(recordWorkETime2 + ", " + recordWorkETime2.equals(workEtime2) );*/
           if(workDate.equals(date) && recordWorkSTime.equals(workStime) && recordWorkSTime2.equals(workStime2) 
               && recordWorkETime.equals(workEtime) && recordWorkETime2.equals(workEtime2)) {
             logger.info("添加工作时间成功");
           }else {
             status = false;
             logger.error("添加工作时间失败");
           }
         }catch(JSONException e) {
           throw new AutoException("解析成json对象时出错");
         }
       }else {
         
         throw new AutoException("设置工作时间失败");
       }
     }catch(Exception e) {
       throw new AutoException("设置工作时间失败");
     }
    
  }
}
